﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;

namespace JsonEditor
{
    public partial class JsonForm : Form
    {
        string path
        {
            get
            {
                return Path.Combine(Application.StartupPath, "81206.json");
            }
        }
        public JsonForm()
        {
            InitializeComponent();
        }

        private void JsonForm_Load(object sender, EventArgs e)
        {
            //            var json = @"{
            //""parent"":""builtin/generated"",
            //""textures"": {
            //   ""layer0"":""mm:items/iron_dust""
            //},
            //""display"": {      
            //       ""scale"": [ 1.7, 1.7, 1.7 ]
            // }
            //}";
            StreamReader reader = new StreamReader(path);
            string json = reader.ReadToEnd();
            reader.Close();
            JObject obj = JObject.Parse(json);
            getAllProperties(obj);
        }

        void getAllProperties(JToken children)
        {
            if (treeView1.Nodes.Count == 0)
                treeView1.Nodes.Add("Root");            
            TreeNode mainNode = treeView1.Nodes[0];
            mainNode.Text = Path.GetFileNameWithoutExtension(path);
            foreach (JToken child in children.Children())
            {
                var property = child as JProperty;
                if (property != null)
                {
                    if (property.Value.Type == JTokenType.String)
                    {
                        TreeNode keyNode = mainNode.Nodes.Add(property.Name);
                        keyNode.Nodes.Add(property.Value.ToString());
                    }
                    if (property.Value.Type == JTokenType.Array)
                    {
                        JArray array = (JArray)property.Value;
                        TreeNode node = mainNode.Nodes.Add(property.Name);
                        for (int i = 0; i < array.Count; i++)
                        {
                            node.Nodes.Add(array[i].ToString());
                        }
                    }
                    if (property.Value.Type == JTokenType.Object)
                    {
                        TreeNode topNode = mainNode.Nodes.Add(property.Name.ToString());
                        foreach (var item in property)
                        {
                            if (item.Type == JTokenType.String)
                            {
                                if (property.Value.Type == JTokenType.String)
                                {
                                    TreeNode keyNode = topNode.Nodes.Add(property.Name);
                                    keyNode.Nodes.Add(property.Value.ToString());
                                }
                                if (property.Value.Type == JTokenType.Array)
                                {
                                    JArray array = (JArray)property.Value;
                                    TreeNode node = topNode.Nodes.Add(property.Name);
                                    for (int i = 0; i < array.Count; i++)
                                    {
                                        node.Nodes.Add(array[i].ToString());
                                    }
                                }
                            }
                        }
                    }
                    // Console.WriteLine(property.Name +":" + property.Value);//print all of the values
                }
                getAllProperties(child);
                treeView1.ExpandAll();
            }
        }
    }
}
